import controlP5.*;

public class Menu {
	
	//CHECK BOXES
	CheckBox CB_Military;
	CheckBox CB_UfoType;
	CheckBox CB_Airport;
	CheckBox CB_Population;
	CheckBox CB_Weather;
	
	//DROPDOWN LISTS
	DropdownList DL_Month1, DL_Day1, DL_Year1, 
	  			 DL_Month2, DL_Day2, DL_Year2;
	
	//BUTTONS
	controlP5.Button B_ViewGraph;
	controlP5.Button B_ViewMap;
	controlP5.Button B_Update;
	
	//RADIO BUTTONS
	RadioButton RB_Graph;
	
	//NUMBER BOXES
	Textfield month1, month2, day1, day2, year1, year2;
	
	public Menu() {
		B_Update = GLOBAL.controlP5.addButton("update", 0, 465, 60, 60, 20);
			B_Update.setLabel("Update Map");
	
		B_ViewGraph = GLOBAL.controlP5.addButton("bgraph", 1, 102, 80, 100, 20);
			B_ViewGraph.setLabel("Graph View");
		
		B_ViewMap = GLOBAL.controlP5.addButton("bmap", 1, 0, 80, 100, 20);
			B_ViewMap.setLabel("Map View");
		
		CB_Military = GLOBAL.controlP5.addCheckBox("military", 895, 30);
			CB_Military.addItem("Show Military Bases", 0);
			CB_Military.setColorLabel(0);
			
		CB_UfoType = GLOBAL.controlP5.addCheckBox("ufoType", 700, 30);
			CB_UfoType.addItem("Unknown", 0);
			CB_UfoType.addItem("Object", 1);
			CB_UfoType.addItem("Light-Based", 2);
			CB_UfoType.addItem("Triangular", 3);
			CB_UfoType.addItem("Rectangular", 4);
			CB_UfoType.addItem("Circular", 5);
			CB_UfoType.setColorLabel(0);
			CB_UfoType.setSpacingColumn(70);
			CB_UfoType.setItemsPerRow(2);
			CB_UfoType.activateAll();
			
		CB_Airport = GLOBAL.controlP5.addCheckBox("airport", 895, 46);
			CB_Airport.addItem("Show Airports", 0);
			CB_Airport.setColorLabel(0);
			
		CB_Population = GLOBAL.controlP5.addCheckBox("population", 895, 62);
			CB_Population.addItem("Show Population Density", 0);
			CB_Population.setColorLabel(0);
			
		CB_Weather = GLOBAL.controlP5.addCheckBox("weather", 895, 78);
			CB_Weather.addItem("Show Weather Stations", 0);
			CB_Weather.setColorLabel(0);
			
		DL_Month1 = GLOBAL.controlP5.addDropdownList("month1", 343, 40, 60, 70);
			addMonths(DL_Month1);
			
		DL_Day1 = GLOBAL.controlP5.addDropdownList("day1", 405, 40, 30, 70);
			addDays(DL_Day1, 1, 2001);
			
		DL_Year1 = GLOBAL.controlP5.addDropdownList("year1", 437, 40, 40, 70);
			addYears(DL_Year1);
		
		DL_Month2 = GLOBAL.controlP5.addDropdownList("month2", 514, 40, 60, 70);
			addMonths(DL_Month2);
			
		DL_Day2 = GLOBAL.controlP5.addDropdownList("day2", 576, 40, 30, 70);
			addDays(DL_Day2, 5, 2004);
		
		DL_Year2 = GLOBAL.controlP5.addDropdownList("year2", 608, 40, 40, 70);
			addYears(DL_Year2);	
			
		RB_Graph = GLOBAL.controlP5.addRadioButton("graph_buttons", 40, 620);
			RB_Graph.addItem("Distance from Military Base", 0);
			RB_Graph.addItem("Distance from Airport", 1);
			RB_Graph.addItem("Distance from Weather Station", 2);
			RB_Graph.addItem("Time of Day", 3);
			RB_Graph.addItem("Time of Year", 4);
			RB_Graph.addItem("Year by Year (2001-2010)", 5);
			RB_Graph.setColorLabel(0);
			RB_Graph.activate(0);
			RB_Graph.setVisible(false);
			
		
		//Map view is active first. Update tab accordingly...
		setActiveTab("bmap");
	}	

	//Returns true when user wishes to see military bases
	public boolean getMilitaryStatus() {
		return CB_Military.getItem(0).value() == 1;
	}
	
	//Returns true when user wishes to see population density
	public boolean getPoplulationStatus() {
		return CB_Population.getItem(0).value() == 1;
	}
	
	//Returns true when user wishes to see airports
	public boolean getAirportStatus() {
		return CB_Airport.getItem(0).value() == 1;
	}
	
	public boolean getWeatherStatus() {
		return CB_Weather.getItem(0).value() == 1;
	}
	
	public void updateSelectedTypes() {
		for(int x=0; x<6; ++x)
			GLOBAL.selectedTypes[x] = (int) CB_UfoType.getItem(x).value();
	}
	
	//Populate a DropdownList with the months of the year.
	private void addMonths(DropdownList d) {
		d.setLabel("Month");
		d.addItem("January", 1);
		d.addItem("February", 2);
		d.addItem("March", 3);
		d.addItem("April", 4);
		d.addItem("May", 5);
		d.addItem("June", 6);
		d.addItem("July", 7);
		d.addItem("August", 8);
		d.addItem("September", 9);
		d.addItem("October", 10);
		d.addItem("November", 11);
		d.addItem("December", 12);
	}
	
	//Populate DropdownList with years 2001-2010
	private void addYears(DropdownList d) {
		d.setLabel("Year");
		
		for(int i=2001; i<2011; ++i)
			d.addItem(String.valueOf(i), i);
	}
	
	//Populate DropdownList with the days of a month
	//	arg "m" = 1-12 correspoding to month
	//	arg "y" = 2001-2010 correspoding to year
	private void addDays(DropdownList d, int m, int y) {
		int old = (int) d.value();
		d.clear();
		
		for(int i=1; i<32; ++i) {
			if(i < 29)		//The first 28 days are always valid
				d.addItem(String.valueOf(i), i);
			else if(isValidDate(i,m,y))	//days 29-31 only occur in certain months
				d.addItem(String.valueOf(i), i);
		}
		
		if(old == 0)
			d.setLabel("Day");
		else
			d.setLabel("" + old);
	}
	
	public void updateDays(int group) {
		switch(group){
		case 1: addDays(DL_Day1, (int) DL_Month1.value(), (int) DL_Year1.value());
		case 2:	addDays(DL_Day2, (int) DL_Month2.value(), (int) DL_Year2.value());
		}
	}
	
	//Range is valid when the first date comes before the second date (or if they are equal)
	public boolean isValidRange() {
		int day1 = (int) DL_Day1.value();
		int day2 = (int) DL_Day2.value();
		
		if(DL_Day1.value() == 0.0) {
			if(DL_Day1.captionLabel().toString().equals("Day"))
				return false;
			day1 = Integer.parseInt(DL_Day1.captionLabel().toString());
		}
		if(DL_Day2.value() == 0.0) {
			if(DL_Day2.captionLabel().toString().equals("Day"))
				return false;
			day1 = Integer.parseInt(DL_Day2.captionLabel().toString());
		}
		
		return ((DL_Year1.value() < DL_Year2.value()) ||
				((DL_Year1.value() == DL_Year2.value()) &&
						((DL_Month1.value() < DL_Month2.value()) ||
								((DL_Month1.value() == DL_Month2.value()) &&
										((day1 < day2) || day1 == day2)) )));
	}
	
	public void updateGlobals() {
		GLOBAL.DATE_START.set((int) DL_Year1.value(), (int) DL_Month1.value(), (int) DL_Day1.value());
		GLOBAL.DATE_END.set((int) DL_Year2.value(), (int) DL_Month2.value(), (int) DL_Day2.value());
	}
	
	//Return true if date exists (eg. Feb 31st is false)
	private boolean isValidDate(int day, int month, int year) {
		if(day > 0 && day < 32) {
			//February cases
			if(month == 2)
				return ((year == 2004 || year == 2008) && day < 30) || (day < 29);
			
			//Months with 30 days
			if(month == 4 || month == 6 || month == 9 || month == 11)
				return (day < 31);
			
			return true;
		}
		return false;
	}
	
	//Color active tab (map or graph button)
	public void setActiveTab(String arg0) {
		//Graph is active
		if(arg0.equals("bgraph")) {
			//Background colors
			B_ViewGraph.setColorBackground(GLOBAL.colorButtonActive);
			B_ViewMap.setColorBackground(GLOBAL.colorButtonInactive);
			
			//Show buttons for graph selection
			RB_Graph.setVisible(true);

			return;
		}
		
		//Map is active
		//Background colors:
		B_ViewMap.setColorBackground(GLOBAL.colorButtonActive);
		B_ViewGraph.setColorBackground(GLOBAL.colorButtonInactive);
		//Hide buttons for graph selection
		RB_Graph.setVisible(false);
	}
}
